//
//  MYMySetBindingAccountsVC.m
//  MuYing
//
//  Created by tbb on 2019/8/19.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMySetBindingAccountsVC.h"

@interface MYMySetBindingAccountsVC ()
@property (nonatomic, strong) UIButton * codeButton;
@property (nonatomic, strong) Calculagraph *codeTimer;
@end

@implementation MYMySetBindingAccountsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.rightItemTitle = kYQLS(@"完成");
}

-(void)rightItemAction:(UIButton *)sender {
    MYMySetRevampCodeCell *phoneCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    MYMySetRevampCodeTextFieldCell *codeCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    
    NSString *url ;
    NSMutableDictionary *dic = NSMutableDictionary.new;

    if (phoneCell.textField.text.length == 0 && self.type == 0) {
        [MBProgressHUD showSuccess:kYQLS(@"请输入手机号")];
        return;
    }
    if (phoneCell.textField.text.length == 0 && self.type == 1) {
        [MBProgressHUD showSuccess:kYQLS(@"请输入邮箱")];
        return;
    }
    
    if (codeCell.textField.text.length == 0) {
        [MBProgressHUD showSuccess:kYQLS(@"请输入验证码")];
        return;
    }
    
    if (self.type == 0) {
        url = kPOST_BIND_PHONE_URL;
        dic[@"mobile"] = phoneCell.textField.text;

    }else{
        url = kPOST_BIND_EMAIL_URL;
        dic[@"email"] = phoneCell.textField.text;

    }
    dic[@"code"] = codeCell.textField.text;

    
    [YQPNetWorkingManager sendPostRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (responseObject.code == 0) {
            [MBProgressHUD showSuccess:kYQLS(@"绑定成功")];
            [self.navigationController popToRootViewControllerAnimated:YES];

        }else{
            [MBProgressHUD showSuccess:kYQLS(responseObject.msg)];

        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {

    }];
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0XFAFAFA);
    kYQREGISTERTABLEVIEWCELL([MYMySetRevampCodeCell class])
    kYQREGISTERTABLEVIEWCELL([MYMySetRevampCodeTextFieldCell class])
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYMySetBindingAccountsFooter")
}
-(void)setType:(NSInteger)type {
    _type = type;
    if (type == 0) {
        self.navigationItem.title = kYQLS(@"绑定手机号");
    } else {
        self.navigationItem.title = kYQLS(@"绑定邮箱");
    }
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        MYMySetRevampCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetRevampCodeCell"];
        
        if (self.type == 0) {
            cell.placeholderStr = kYQLS(@"请输入手机号");
        } else {
            cell.placeholderStr = kYQLS(@"请输入邮箱号");
        }
        [cell.codeBtn addTarget:self action:@selector(codeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        self.codeButton = cell.codeBtn;
        return cell;
    }else {
        MYMySetRevampCodeTextFieldCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetRevampCodeTextFieldCell"];
        cell.placeholderStr = kYQLS(@"验证码");
        return cell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 58;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    MYMySetBindingAccountsFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYMySetBindingAccountsFooter"];
    if (self.type == 0) {
        footer.titleLb.text =  kYQLS(@"请输入新手机号进行绑定");
    } else {
        footer.titleLb.text =  kYQLS(@"请输入新邮箱进行绑定");
    }
    return footer;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

#pragma mark - 获取短信验证码
- (void)codeBtnClick:(UIButton *)button
{
    MYMySetRevampCodeCell *phoneCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    MYMySetRevampCodeTextFieldCell *codeCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    
    NSString *url ;
    NSMutableDictionary *dic = NSMutableDictionary.new;

    if (self.type == 0) {
        url = kGET_CODE_SMS_URL;
        dic[@"phone"] = phoneCell.textField.text;

    }else{
        url = kGET_EMAIL_SEND_URL;
        dic[@"email"] = phoneCell.textField.text;

    }
    if (phoneCell.textField.text.length == 0 && self.type == 0) {
        [MBProgressHUD showSuccess:kYQLS(@"请输入手机号")];
        return;
    }
    if (phoneCell.textField.text.length == 0 && self.type == 1) {
        [MBProgressHUD showSuccess:kYQLS(@"请输入邮箱")];
        return;
    }

    [YQPNetWorkingManager sendGetRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [self.codeTimer start];
        [MBProgressHUD showSuccess:kYQLS(@"发送成功")];
        //codeCell.textField.text = responseObject.data;
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
    
}

#pragma mark 计时器KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"time"]) {
        NSInteger seconds = [[change objectForKey:@"new"] integerValue];
        NSString *time = [NSString stringWithFormat:@"%zdS",60-seconds];
        self.codeButton.enabled = NO;
        [self.codeButton setTitle:time forState:UIControlStateDisabled];
        
        if ([time isEqualToString:@"0S"]) {
            [self.codeTimer stop];
            self.codeButton.enabled = YES;
            [self.codeButton setTitle:kYQLS(@"重新获取") forState:(UIControlStateNormal)];
            [self.listTableView reloadData];
        }
    }
}

#pragma mark-- lazy
- (Calculagraph *)codeTimer {
    if (!_codeTimer) {
        _codeTimer = [[Calculagraph alloc] init];
        [_codeTimer addObserver:self forKeyPath:@"time" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _codeTimer;
}
#pragma mark-- lazy
- (void)dealloc {
    [self.codeTimer removeObserver:self forKeyPath:@"time"];
}
@end
